package com.buaa.controller;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buaa.service.BaseService;
import com.buaa.utils.JacksonUtil;
import com.buaa.utils.PieChart3D;

/**
 * Title: 基地控制器<br>
 * Description: <br>
 * Company: BUAA
 *
 * @author 伟宏
 * @date 2017年5月6日
 * @modify 2017年05月06日
 */
@Controller
@RequestMapping("base")
public class BaseController {

	@Autowired
	private BaseService baseService;

	/**
	 * 统计信息界面
	 */
	@RequestMapping("syjd")
	public String getBase() {
		return "syjd";
	}

	/**
	 * 统计信息服务
	 * 
	 * @param zhibiao
	 * @param curlevel
	 * @param curcenterlng
	 * @param curcenterlat
	 * @param loctype
	 * @param session
	 * @return
	 */
	@RequestMapping("getSum")
	@ResponseBody
	public String getSum(String zhibiao, String curlevel, String curcenterlng, String curcenterlat, String loctype,
			HttpSession session) {
		session.setAttribute("curlevel", curlevel);
		session.setAttribute("curcenterlng", curcenterlng);
		session.setAttribute("curcenterlat", curcenterlat);
		return baseService.sumInfo(loctype);
	}

	/**
	 * 统计信息服务 - 获取柱状图
	 * 
	 * @param quyu
	 * @param loctype
	 * @param zhibiao
	 * @param session
	 * @return
	 */
	@RequestMapping("getCount")
	@ResponseBody
	public String getCount(String quyu, String loctype, String zhibiao, HttpSession session) {
		Map<String, Long> metaData = baseService.getCount(quyu, loctype, zhibiao);
		String filename = "";
		try {
			filename = PieChart3D.generateRoChart(metaData, 600, 360, session, new PrintWriter("asdf"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String graphURL = "/Servlet/DisplayChart?filename=" + filename;
		return graphURL;
	}

	/**
	 * 内容搜索界面
	 */
	@RequestMapping("syjdcx")
	public ModelAndView getSyjdcx() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("syjdcx");
		mav.addObject("baseLevelList", baseService.findBaseLevel());
		mav.addObject("baseTypeList", baseService.findBaseType());
		return mav;
	}

	/**
	 * 内容搜索服务
	 * 
	 * @param loctype
	 * @param jdmc
	 * @param jdmccxfs
	 * @param jdjb
	 * @param jdlx
	 * @return
	 */
	@RequestMapping("getInstitution")
	@ResponseBody
	public String getBase(String loctype, String jdmc, String jdmccxfs, String jdjb, String jdlx,String ssjd,String ssjdcxfs) {
//		return baseService.contentSearch(loctype, jdmc, jdmccxfs, jdjb, jdlx);
		return baseService.contentSearch2(loctype, jdmc, jdmccxfs, jdjb, jdlx,ssjd,ssjdcxfs);
	}

	/**
	 * 获取alllist信息
	 * 
	 * @param loctype
	 * @return
	 */
	@RequestMapping("syjdalllist")
	public ModelAndView getAllList(String loctype) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/syjdalllist");
		mav.addObject("loctype", loctype);
		mav.addObject("zhibiao", "city");
		if (loctype.equals("city")) {
			mav.addObject("zhibiao", "所在市");
		} else if (loctype.equals("danwei")) {
			mav.addObject("zhibiao", "所属单位");
		}
		List<Object[]> ret = baseService.getAllList(loctype);
		if (ret != null) {
			mav.addObject("AllList", ret);
		}
		return mav;
	}

	/**
	 * 获取countlist信息
	 * 
	 * @param quyu
	 * @param loctype
	 * @param zhibiao
	 * @return
	 */
	@RequestMapping("syjdcountlist")
	public ModelAndView getCountList(String quyu, String loctype, String zhibiao) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/syjdcountlist");
		mav.addObject("zhibiao", zhibiao);
		mav.addObject("quyu", quyu);
		mav.addObject("loctype", loctype);
		List<Object> ret = baseService.getCountList(quyu, loctype, zhibiao);
		if (ret != null) {
			mav.addObject("name", ret.get(0));
			mav.addObject("CountList", ret.get(1));
		}
		return mav;
	}

	/**
	 * 获取cx表1数据
	 * 
	 * @param jdmc
	 * @param jdmccxfs
	 * @param jdjb
	 * @param jdlx
	 * @param quyu
	 * @param loctype
	 * @return
	 */
	@RequestMapping("syjdcxtable1")
	public ModelAndView getCxTable1Info(String jdmc, String jdmccxfs, String jdjb, String jdlx, String quyu,
			String loctype,String ssjd,String ssjdcxfs) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/syjdcxtable1");
		//mav.addObject("tableInfoList", baseService.getCxTable1Info(jdmc, jdmccxfs, jdjb, jdlx, quyu, loctype));
		mav.addObject("tableInfoList", baseService.getCxTable1Info2(jdmc, jdmccxfs, jdjb, jdlx, quyu, loctype,ssjd,ssjdcxfs));
		return mav;
	}

	/**
	 * 获取表1数据,ssjd,ssjdcxfs
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("syjdtable1")
	public ModelAndView getTable1Info(String zhibiao, String zhibiaomc, String quyu, String loctype, String page) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/syjdtable1");
		mav.addObject("zhibiao", zhibiao);
		mav.addObject("zhibiaomc", zhibiaomc);
		mav.addObject("quyu", quyu);
		mav.addObject("loctype", loctype);
		List<Object> ret = baseService.getTable1Info(zhibiao, zhibiaomc, quyu, loctype, page);
		if (ret != null) {
			mav.addObject("totalpage", ret.get(0));
			mav.addObject("total", ret.get(1));
			mav.addObject("curpage", ret.get(2));
			mav.addObject("nextpage", ret.get(3));
			mav.addObject("lastpage", ret.get(4));
			mav.addObject("pagenum", ret.get(5));
			mav.addObject("beginnum", ret.get(6));
			mav.addObject("endnum", ret.get(7));
			mav.addObject("tableInfoList", ret.get(8));
		}
		return mav;
	}
	
	/**
	 * 获取表2数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("syjdtable2")
	public ModelAndView getTable2Info(String id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/syjdtable2");
		List<Object[]> ret = baseService.getTable2Info(id);
		if (ret != null) {
			Object[] xm = ret.get(0);
			mav.addObject("name", xm[0]);
			mav.addObject("code", xm[1]);
			mav.addObject("level", xm[2]);
			mav.addObject("type", xm[3]);
			mav.addObject("city", xm[4]);
			mav.addObject("county", xm[5]);
			mav.addObject("ins_name", xm[6]);
			mav.addObject("ins_code", xm[7]);
		}
		return mav;
	}
	
	/**
	 * 获取比较的数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("syjdcp")
	public ModelAndView getCp(String totalid,HttpSession session) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/syjdcp");
		String[][] data = baseService.getCp(totalid);
		mav.addObject("data", data);
		String filename = "";
		try {
			filename = PieChart3D.generateRoChartsyjdcp(data, 600, 375, session,new PrintWriter("pic"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String graphURL = "/Servlet/DisplayChart?filename=" + filename;
		mav.addObject("graphURL", graphURL);
		return mav;
	}
	
	@RequestMapping("indexAnalysis")
	public ModelAndView getIndex() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/indexAnalysis");
		mav.addObject("resource", "base");
		return mav;
	}
	
	@RequestMapping("sum")
	@ResponseBody
	public String getSum(String zhibiao) {
		return baseService.getSum(zhibiao);
	}


	@RequestMapping("fb")
	@ResponseBody
	public String getFb() {
		return JacksonUtil.toJSon(baseService.getFb());
	}

	@RequestMapping("fb2")
	@ResponseBody
	public String getFb2() {
		return JacksonUtil.toJSon(baseService.getFb2());
	}
	
	
	/**
	 * 统计信息服务 - 汇总
	 * 
	 * @return
	 */
	@RequestMapping("citySum")
	@ResponseBody
	public String getCitySum() {
		return baseService.getCitySum();
	}
}
